<?php

/**
 * 框架核心类文件
 * Created by PhpStorm.
 * User: simon_min@aliyun.com
 * Date: 2016/11/5
 * Time: 11:35
 */
Final class Framework
{
    /**
     * 设置框架系统常量
     */
    private static function _set_const()
    {
        $path = str_replace('\\', '/', __FILE__);
        define('FRAMEWORK_PATH', dirname($path));
        define('CONFIG_PATH', FRAMEWORK_PATH . '/Config');
        define('DATA_PATH', FRAMEWORK_PATH . '/Data');
        define('LIB_PATH', FRAMEWORK_PATH . '/Lib');
        define('CORE_PATH', LIB_PATH . '/Core');
        define('FUNCTION_PATH', LIB_PATH . '/Function');

        define('EXTENDS_PATH', FRAMEWORK_PATH . '/Extends');
        define('ORG_PATH', EXTENDS_PATH . '/Org');
        define('TOOL_PATH', EXTENDS_PATH . '/Tool');

        define('ROOT_PATH', dirname(FRAMEWORK_PATH));
        //临时目录
        define('TEMP_PATH', ROOT_PATH . '/Temp');
        define('APP_COMPILE_PATH', TEMP_PATH . '/' . APP_NAME . '/Compile');
        define('APP_CACHE_PATH', TEMP_PATH . '/' . APP_NAME . '/Cache');

        //日志目录
        define('LOG_PATH', TEMP_PATH . '/Log');
        //应用目录
        define('APP_PATH', ROOT_PATH . '/' . APP_NAME);
        define('APP_CONFIG_PATH', APP_PATH . '/Config');
        define('APP_CONTROLLER_PATH', APP_PATH . '/Controller');
        define('APP_TPL_PATH', APP_PATH . '/Tpl');
        define('APP_PUBLIC_PATH', APP_TPL_PATH . '/Public');

        //创建公共
        define('COMMON_PATH', ROOT_PATH . '/Common');
        //公共配置项文件夹
        define('COMMON_CONFIG_PATH', COMMON_PATH . '/Config');
        //公共模型文件夹
        define('COMMON_MODEL_PATH', COMMON_PATH . '/Model');
        //公共库文件夹
        define('COMMON_LIB', COMMON_PATH . '/Lib');
        //公共资源目录
        define('ASSETS', ROOT_PATH . '/Public');
        //公共上传文件目录
        define('UPLOADS', ROOT_PATH . '/Uploads');

        //判断是否是POST请求
        define('IS_POST', $_SERVER['REQUEST_METHOD'] == 'POST' ? true : false);
        //判断是否是AJAX请求
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
            define('IS_AJAX', true);
        } else {
            define('IS_AJAX', false);
        }
    }

    /**
     * 创建应用所需目录
     */
    private static function _create_dir()
    {
        if (is_writable(ROOT_PATH)) {
            //生成项目目录文件
            $arr = array(
                ASSETS,
                UPLOADS,
                COMMON_CONFIG_PATH,
                COMMON_MODEL_PATH,
                COMMON_LIB,
                APP_PATH,
                APP_CONFIG_PATH,
                APP_CONTROLLER_PATH,
                APP_TPL_PATH,
                APP_PUBLIC_PATH,
                TEMP_PATH,
                APP_COMPILE_PATH,
                APP_CACHE_PATH,
                LOG_PATH
            );
            foreach ($arr as $v) {
                is_dir($v) || mkdir($v, 0777, true);
            }
            //生成目录安全文件
            if (defined('BUILD_DIR_SECURE') && BUILD_DIR_SECURE) {
                defined('DIR_SECURE_FILENAME') or define('DIR_SECURE_FILENAME', 'index.html');
                defined('DIR_SECURE_CONTENT') or define('DIR_SECURE_CONTENT', ' ');
                // 自动写入目录安全文件
                $content = DIR_SECURE_CONTENT;
                $files = explode(',', DIR_SECURE_FILENAME);
                foreach ($files as $filename) {
                    foreach ($arr as $dir)
                        file_put_contents($dir . '/' . $filename, $content);
                }
            }

            //拷贝框架的相关提示页面
            is_file(APP_TPL_PATH . '/success.html') || copy(DATA_PATH . '/Tpl/success.html', APP_TPL_PATH . '/success.html');
            is_file(APP_TPL_PATH . '/error.html') || copy(DATA_PATH . '/Tpl/error.html', APP_TPL_PATH . '/error.html');
        } else {
            header('Content-Type:text/html; charset=utf-8');
            exit('项目目录不可写，目录无法自动生成！<BR>请使用项目生成器或者手动生成项目目录~');
        }
    }

    /**
     * 载入框架所需必要文件
     */
    private static function _import_file()
    {
        $fileArr = array(
            CORE_PATH . './Log.class.php',
            FUNCTION_PATH . './function.php',
            ORG_PATH . '/Smarty/Smarty.class.php',
            CORE_PATH . '/View.class.php',
            CORE_PATH . '/Controller.class.php',
            CORE_PATH . '/Application.class.php'
        );
        $str = '';
        foreach ($fileArr as $value) {
            $str .= trim(substr(file_get_contents($value), 5));
            require_once $value;
        }
        $str = "<?php\r\n" . $str;
        file_put_contents(TEMP_PATH . '/~runtime.php', $str) || die('access deny');
    }

    /**
     * 框架核心类执行方法
     */
    public static function run()
    {
        self::_set_const();
        //检测DEBUG，如果未定义默认为false
        defined('DEBUG') || define('DEBUG', false);
        //根据DEBUG开启状态生成所需相关目录
        if (DEBUG) {
            self::_create_dir();
            self::_import_file();
        } else {
            //当DEBUG关闭时，判断是否存在项目目录，不存在则生成
            if (!is_dir(dirname(dirname(__FILE__)) . '/' . APP_NAME)) {
                self::_create_dir();
                self::_import_file();
            } else {
                error_reporting(0);
                require TEMP_PATH . '/~runtime.php';
            }
        }
        //执行应用程序类run方法启动框架
        Application::run();
    }

}

Framework::run();
